
//% color="#e2b539" iconWidth=50 iconHeight=40
namespace BLE_Serial{
    //% block="设置BLE UART从机显示名称[NAME]" blockType="command"
    //% NAME.shadow="string" NAME.defl="ble_uart"
    export function BLE_Serialname(parameter: any, block: any) {
        let name = parameter.NAME.code;
        Generator.addImport("from mpython_ble.application import BLEUART");  
        Generator.addDeclaration("URM09_Trig",`_ble_uart = None\ndef _ble_uart_irq():pass`);  
        Generator.addCode(`_ble_uart = BLEUART(name=bytes(${name}, 'utf-8'))
_ble_uart.irq(handler=_ble_uart_irq)`)

    }

    //% block="设置BLE UART主机连接到[MODE][NAME]" blockType="command"
    //% MODE.shadow="dropdown" MODE.options="MODE"
    //% NAME.shadow="string" NAME.defl="ble_uart"
    export function BLE_Serialrole(parameter: any, block: any) {
        let name = parameter.NAME.code;
        let mode = parameter.MODE.code;
        Generator.addImport("from mpython_ble.application import BLEUART\nimport ubinascii"); 

        Generator.addDeclaration("URM09_Trig",`_ble_uart = None\ndef _ble_uart_irq():pass`); 
        if (`${mode}`==="0"){
            Generator.addCode(`_ble_uart = BLEUART(name=bytes(${name}, 'utf-8'), role=BLEUART.MASTER)
_ble_uart.irq(handler=_ble_uart_irq)`)
        }else{
            Generator.addCode(`_ble_uart = BLEUART(role=BLEUART.MASTER, slave_mac=ubinascii.unhexlify(${name}.lower()))
_ble_uart.irq(handler=_ble_uart_irq)`)
        }
    }

    //% block="BLE UART 已连接?" blockType="boolean"
    export function BLE_Seriaconnect(parameter: any, block: any) {
        Generator.addImport("from mpython_ble.application import BLEUART");   
        Generator.addCode(`(_ble_uart and _ble_uart.is_connected())`)

    }
    //% block="BLE UART 可读字节数量" blockType="reporter"
    export function BLE_Seriaread(parameter: any, block: any) {
        Generator.addImport("from mpython_ble.application import BLEUART");   
        Generator.addCode(`(_ble_uart.any())`)

    }   
    //% block="当BLE UART 接收到数据时" blockType="hat"
     export function BLE_Seriaread1(parameter: any, block: any) { 
        Generator.addEvent(`_ble_uart_recv_`, `_ble_uart_irq`, ``);
        Generator.addCode(`_received_data = bytes(_ble_uart.read())`);

    }
    //% block="BLE UART 消息" blockType="reporter"
    export function BLE_Seriareaddata(parameter: any, block: any) { 
        Generator.addCode(`_received_data`);

    }
    //% block="BLE UART 发送字节[BYTE]" blockType="command"
    //% BYTE.shadow="string" BYTE.defl="abc"
    export function BLE_Seriawritemess(parameter: any, block: any) { 
        let byte = parameter.BYTE.code;
        Generator.addImport("from mpython_ble.application import BLEUART");   
        Generator.addCode(`_ble_uart.write(bytes(${byte}, 'utf-8'))`);

    }
    //% block="BLE UART 断开连接" blockType="command"
    //% BYTE.shadow="string" BYTE.defl="abc"
    export function BLE_Seriaclose(parameter: any, block: any) { 
        Generator.addImport("from mpython_ble.application import BLEUART");   
        Generator.addCode(`_ble_uart.close()`);

    }
}
